package com.jf.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import cn.trinea.android.common.view.DropDownListView;
import cn.trinea.android.common.view.DropDownListView.OnDropDownListener;

import com.jf.map.NearbyServMap;
import com.jf.map.NearbyUser;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.sys.android.util.FinalUntil;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

@ContentView(R.layout.nearby_serv)
public class NearbyServ extends Activity {

	private int[] messImage = FinalUntil.messImage;

	private DropDownListView datalist = null; // 定义ListView组件
	private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); // 定义显示的内容包装
	private SimpleAdapter simpleAdapter = null; // 进行数据的转换操作
	private Integer pageNu = 0;
	private int totalCount;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);// xml视图加载的处理

		this.datalist = (DropDownListView) findViewById(R.id.listView1); // 取得组件
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("pageNu", pageNu.toString());
		HttpUtils http = new HttpUtils();
		http.send(HttpMethod.POST, FinalUntil.ServerAddress + "findSevDot",
				params, new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						Log.i("NearbyServ", arg1);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						Log.i("NearbyServ", arg0.result);
						try {
							JSONArray jsonarr = new JSONArray(arg0.result);
							if(jsonarr.length()>0){
								totalCount = jsonarr.getJSONObject(0).getInt("examineStates");
							}
							for (int x = 0; x < jsonarr.length(); x++) {
								JSONObject jsonobj = jsonarr.getJSONObject(x);
								Map<String, Object> map = new HashMap<String, Object>(); // 定义Map集合，保存每一行数据
								map.put("icon",
										messImage[jsonobj.getInt("brand")]);
								map.put("sendName",
										jsonobj.getString("compary")); // 与data_list.xml中的TextView组加匹配
								map.put("sendcall",
										jsonobj.getString("contactNum")); // 与data_list.xml中的TextView组加匹配
								map.put("sendaddress",
										jsonobj.getString("regAddress")); // 与data_list.xml中的TextView组加匹配
								list.add(map); // 保存了所有的数据行
							}
						} catch (Exception e) {
							Log.i("NearbyServ", e.toString());
						}
						simpleAdapter = new SimpleAdapter(NearbyServ.this,
								list, R.layout.my_sendfilelist, new String[] {
										"icon", "sendName", "sendcall",
										"sendaddress" } // Map中的key的名称
								, new int[] { R.id.icon, R.id.sendName,
										R.id.sendcall, R.id.sendaddress }); // 是data_list.xml中定义的组件的资源ID
						datalist.setAdapter(simpleAdapter);
						datalist.setOnItemClickListener(new OnItemClickListenerImpl()); // 单击选项
						datalist.setFooterDefaultText("加载更多");
						datalist.setOnBottomListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								fleshNearbyServ();
							}
						});
					}
				});

	}

	public void finishNearServ(View view) {
		this.finish();
	}

	public void nearbyServ(View v) {

		Intent intent = new Intent(NearbyServ.this, NearbyServMap.class);
		startActivity(intent);
	}

	private class OnItemClickListenerImpl implements OnItemClickListener {

		@SuppressWarnings("unchecked")
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Map<String, String> map = (Map<String, String>) NearbyServ.this.simpleAdapter
					.getItem(position);
			String sendName = map.get("sendName");
			String sendcall = map.get("sendcall");
			Intent intent = new Intent();
			intent.setAction("android.intent.action.CALL");
			intent.setData(Uri.parse("tel:" + sendcall));
			startActivity(intent);
		}
	}
	/**
	 * 下拉加载
	 */
	public void fleshNearbyServ(){
		
		pageNu = list.size();
		if(pageNu==totalCount){
			datalist.onBottomComplete();
			return;
		}
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("pageNu", pageNu.toString());
		HttpUtils http = new HttpUtils();
		http.send(HttpMethod.POST, FinalUntil.ServerAddress + "findSevDot",
				params, new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						Log.i("NearbyServ", arg1);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						Log.i("NearbyServ", arg0.result);
						try {
							JSONArray jsonarr = new JSONArray(arg0.result);
							for (int x = 0; x < jsonarr.length(); x++) {
								JSONObject jsonobj = jsonarr.getJSONObject(x);
								Map<String, Object> map = new HashMap<String, Object>(); // 定义Map集合，保存每一行数据
								map.put("icon",
										messImage[jsonobj.getInt("brand")]);
								map.put("sendName",
										jsonobj.getString("compary")); // 与data_list.xml中的TextView组加匹配
								map.put("sendcall",
										jsonobj.getString("contactNum")); // 与data_list.xml中的TextView组加匹配
								map.put("sendaddress",
										jsonobj.getString("regAddress")); // 与data_list.xml中的TextView组加匹配
								list.add(map); // 保存了所有的数据行
							}
							simpleAdapter.notifyDataSetChanged();
							datalist.onBottomComplete();
						} catch (Exception e) {
							Log.i("NearbyServ", e.toString());
						}
					}
		});
	}
}
